package com.cn.mmd.controller;

import com.cn.mmd.model.Course;
import com.cn.mmd.model.CourseWeekly;
import com.cn.mmd.model.Student;
import com.cn.mmd.model.StudentApp;
import com.cn.mmd.service.CourseWeeklyService;
import com.cn.mmd.service.StudentCourseService;
import com.cn.mmd.service.StudentService;
import com.cn.mmd.tools.SplitSpring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/18 0018.
 */

@Controller
@RequestMapping(value = "page/stuCou")
public class StudentCourseController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentCourseService studentCourseService;

    @Autowired
    private CourseWeeklyService courseWeeklyService;

    private SplitSpring splitSpring = new SplitSpring();

    @Autowired
    private TokenController tokenController;

    @RequestMapping(value = "/select")
    @ResponseBody
    public synchronized Object StudentCourseSelect(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        String studentId = request.getParameter("studentId");
        String token = request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp != null) {
            Student student = studentService.selectByKey(studentId);
            if (student != null && student.getStudentclass() != null && !student.getStudentclass().equals("")) {
                Course course = new Course();
                course.setCourseclasss(student.getStudentclass());
                List<Course> courseList = studentCourseService.selectClasss(course);
                String week1;
                int n;
                int m;
                List<CourseWeekly> courseWeekly;
                if (courseList != null) {
                    for (int i = 0; i < courseList.size(); i++) {
                        course = courseList.get(i);
                        courseWeekly = courseWeeklyService.selectList(course.getCourseid());
                        String s = course.getCourseteacherid();
                        String s1 = course.getCourseteacher();
                        List<Map> mapList1 = new ArrayList<Map>();
                        String[] strings;
                        String[] strings1;
                        for (int i1 = 0; i1 < s.split("/").length;i1++) {
                            Map<String, Object> map1 = new HashMap<String, Object>();
                            strings = s.split("/");
                            strings1 = s1.split("/");
                            if (strings != null && strings[i1] != null && !strings[i1].equals("")) {
                                map1.put("teacherId", strings[i1]);
                                map1.put("teacherName",strings1[i1]);
                                mapList1.add(map1);
                            }
                        }
                        for (int j = 0; j < courseWeekly.size(); j++) {
                            week1 = courseWeekly.get(j).getCourseweeklyweek();
                            if (!week1.equals("")) {
                                Map<String, Object> map = new HashMap<String, Object>();
                                map.put("weeklyDay", courseWeekly.get(j).getCourseweeklyday());
                                map.put("weeklyQuarter", courseWeekly.get(j).getCourseweeklyquarter());
                                map.put("beginEndWeek", week1);
                                map.put("classRoom", courseWeekly.get(j).getCourseweeklyclassroom());
                                map.put("courseName", course.getCoursename());
                                map.put("courseId", course.getCourseid());
                                map.put("courseWeeklyId", courseWeekly.get(j).getCourseweeklyid() + "");
                                map.put("teachers", mapList1);
                                mapList.add(map);
                            }
                        }
                    }
                }
                if (mapList != null) {
                    objectMap.put("code", 200);
                    objectMap.put("message", "成功");
                    objectMap.put("data", mapList);
                } else {
                    objectMap.put("code", 201);
                    objectMap.put("message", "查看失败");
                }
            } else {
                objectMap.put("code", 202);
                objectMap.put("message", "失败没有这个学生");
            }
        } else {
            objectMap.put("code", 203);
            objectMap.put("message", "此用户没有权限做此操作");
        }

        return objectMap;
    }


}
